package com.sdygmedia.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sdygmedia.dao.AdminDao;
import com.sdygmedia.domain.Admin;
import com.sdygmedia.service.AdminService;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

	@Resource
	private AdminDao adminDao;
	
	@Override
	public List<Admin> findAll() {
		// TODO Auto-generated method stub
		return adminDao.findAll();
	}

	@Override
	public void delete(long id) {
		// TODO Auto-generated method stub
		adminDao.delete(id);
	}

	@Override
	public void save(Admin admin) {
		// TODO Auto-generated method stub
		//添加管理员的时间
		admin.setDate(new Date());
		
		//密码进行md5加密
		String password = admin.getPassword();
		password = DigestUtils.md5Hex(password);
		admin.setPassword(password);
		
		adminDao.save(admin);
	}

	@Override
	public Admin getById(long id) {
		// TODO Auto-generated method stub
		return adminDao.getById(id);
	}

	@Override
	public void update(Admin admin) {
		// TODO Auto-generated method stub
		
		admin.setDate(new Date());
		//修改信息，密码也需要加密
		String password = admin.getPassword();
		password = DigestUtils.md5Hex(password);
		admin.setPassword(password);
		
		adminDao.update(admin);
	}

	@Override
	public boolean login(String name, String password) {
		// TODO Auto-generated method stub
		Admin admin = adminDao.findByName(name);
		password = DigestUtils.md5Hex(password);
		
		if (admin == null) {
			return false;
		} else if (password.equals(admin.getPassword())){
			return true;
		} else {
			return false;
		}
	}

	
}
